import { eventMgr } from "../Manager/EventMgr"
import { logMgr } from "../Manager/LogMgr"
import { BaseRes } from "./BaseRes"
import { BaseTimer } from "./BaseTimer"
import { CycleLifeFlag } from "./CycleLifeFlag"

export class BaseView extends cc.Component {

    //////////////////////////////////////////////////////框架相关
    private _cycleLifeFlag = 0
    protected setCycleLifeFlag(flag: CycleLifeFlag) {
        this._cycleLifeFlag = this._cycleLifeFlag | flag
    }
    public checkCycleLifeFlag(flag: CycleLifeFlag): boolean {
        return !!(this._cycleLifeFlag & flag)
    }


    private _timer: BaseTimer = null
    protected get Timer(): BaseTimer {
        if (!this._timer) {
            this._timer = new BaseTimer(this)
        }
        return this._timer
    }

    private _baseRes: BaseRes = null
    protected get Res(): BaseRes {
        if (!this._baseRes) {
            this._baseRes = new BaseRes(this)
        }
        return this._baseRes
    }

    protected onDispose(): void {
        this._timer?.stopAllTimer()
        this._baseRes?.destroy()
        this._scriptChildren.forEach((value) => {
            value.onDispose()
        })
    }

    private _scriptChildren: BaseView[] = []
    protected addScriptChildren(child: BaseView) {
        this._scriptChildren.push(child)
    }


    //////////////////////////////////////////////////////功能拓展
    async delay() {
        return new Promise((resolve, reject) => {
            resolve(true)
        })
    }

    protected addCustomEvent(eventName: string, callback: Function) {
        eventMgr.on(eventName, callback, this)
    }

    protected getAutoUI<T extends cc.Component>(type: { prototype: T }): T {
        return this.node.getComponent(type)
    }


    public setSprite(sprite: cc.Sprite, path: string) {
        if (!sprite) return
        this.Res.load(path, cc.SpriteFrame).then((res: cc.SpriteFrame) => {
            sprite.spriteFrame = res
        })
    }

    public setSpine(node: cc.Node, path: string, animName: string, loop: boolean, callback?: Function, target?: any) {
        let spine = node.getComponent(sp.Skeleton)
        if (!spine) return
        this.Res.load(path, sp.SkeletonData).then((res: sp.SkeletonData) => {
            spine.skeletonData = res
            spine.setAnimation(0, animName, loop)
            if (!loop) {
                spine.setCompleteListener(() => {
                    spine.node.active = false
                    if (callback)
                        callback.call(target)
                })
            }
        })
    }

    addClick(btn: cc.Button, callback: Function, soundPath: any = "") {
        if (!btn) {
            logMgr.log(`addClick btn null`)
            return
        }
        if (btn["hasAddClick"]) {
            logMgr.log(`addClick many times`)
            return
        }

        this["isBtnClick"] = false
        let self = this
        let func = function () {
            if (self["isBtnClick"]) {
                logMgr.log("isBtnClick = " + this["isBtnClick"])
                return
            }
            self["isBtnClick"] = true
            setTimeout(function () {
                self["isBtnClick"] = false
            }, 100)
            callback.call(self, btn)
        }
        btn.node.on("click", func, this);
        btn["hasAddClick"] = true
    }


}
